package com.patentbackend.patentmanage.utils;

import com.patentbackend.patentmanage.entity.Patent;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

public class ExcelUtils {

    public static ByteArrayInputStream patentsToExcel(List<Patent> patents) throws IOException {
        String[] HEADERS = {
                "ID", "专利标题", "专利号", "专利类型", "专利状态",
                "归属单位", "申请人", "发明人", "申请日期",
                "授权日期", "年费截止日期", "年费是否已缴", "创建时间"
        };

        try (Workbook workbook = new XSSFWorkbook();
             ByteArrayOutputStream out = new ByteArrayOutputStream()) {

            Sheet sheet = workbook.createSheet("专利列表");

            // 创建表头行
            Row headerRow = sheet.createRow(0);
            for (int col = 0; col < HEADERS.length; col++) {
                Cell cell = headerRow.createCell(col);
                cell.setCellValue(HEADERS[col]);
            }

            // 设置日期格式
            CellStyle dateCellStyle = workbook.createCellStyle();
            CreationHelper createHelper = workbook.getCreationHelper();
            dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd"));

            // 填充数据
            int rowIdx = 1;
            for (Patent patent : patents) {
                Row row = sheet.createRow(rowIdx++);

                row.createCell(0).setCellValue(patent.getId());
                row.createCell(1).setCellValue(patent.getTitle());
                row.createCell(2).setCellValue(patent.getPatentNumber());
                row.createCell(3).setCellValue(patent.getType().toString());
                row.createCell(4).setCellValue(patent.getStatus().toString());
                row.createCell(5).setCellValue(patent.getOrganization());
                row.createCell(6).setCellValue(patent.getApplicant());
                row.createCell(7).setCellValue(patent.getInventor());

                Cell applicationDateCell = row.createCell(8);
                if (patent.getApplicationDate() != null) {
                    applicationDateCell.setCellValue(patent.getApplicationDate());
                    applicationDateCell.setCellStyle(dateCellStyle);
                }

                Cell grantDateCell = row.createCell(9);
                if (patent.getGrantDate() != null) {
                    grantDateCell.setCellValue(patent.getGrantDate());
                    grantDateCell.setCellStyle(dateCellStyle);
                }

                Cell annualFeeDueDateCell = row.createCell(10);
                if (patent.getAnnualFeeDueDate() != null) {
                    annualFeeDueDateCell.setCellValue(patent.getAnnualFeeDueDate());
                    annualFeeDueDateCell.setCellStyle(dateCellStyle);
                }

                row.createCell(11).setCellValue(patent.getAnnualFeePaid() ? "是" : "否");

                Cell createTimeCell = row.createCell(12);
                if (patent.getCreateTime() != null) {
                    createTimeCell.setCellValue(patent.getCreateTime());
                    createTimeCell.setCellStyle(dateCellStyle);
                }
            }

            // 自动调整列宽
            for (int i = 0; i < HEADERS.length; i++) {
                sheet.autoSizeColumn(i);
            }

            workbook.write(out);
            return new ByteArrayInputStream(out.toByteArray());
        }
    }
}